﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using TakeoutExpress.Support.Controllers;
using TakeoutExpress.Support.Linq;
namespace TakeoutExpress2008.Web.MVC.Admin
{
    public partial class SiteEditor : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            gvSites.RowUpdated += new GridViewUpdatedEventHandler(gvSites_RowUpdated);
          //  gvSites.RowCommand += new GridViewCommandEventHandler(gvSites_RowCommand);
            gvSites.RowDeleted += new GridViewDeletedEventHandler(gvSites_RowDeleted);
            gvSites.RowDeleting += new GridViewDeleteEventHandler(gvSites_RowDeleting);
            gvSites.PageIndexChanging += new GridViewPageEventHandler(gvSites_PageIndexChanging);
            gvSites.RowEditing += new GridViewEditEventHandler(gvSites_RowEditing);
            gvSites.RowUpdating += new GridViewUpdateEventHandler(gvSites_RowUpdating);
            gvSites.RowCancelingEdit += new GridViewCancelEditEventHandler(gvSites_RowCancelingEdit);
            
            if (!Page.IsPostBack)
            {
                
                PopulateSites();
            }
        }

        private void PopulateSites()
        {
            gvSites.DataSource = new SiteController().GetAllSites();
            gvSites.DataBind();
        }

        void gvSites_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            e.Cancel = false;
            gvSites.EditIndex = -1;
            PopulateSites();
        }

        void gvSites_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            SiteController controller = new SiteController();
            int key = (int)this.gvSites.DataKeys[e.RowIndex].Value;
            TakeoutExpress.Support.Linq.Site site = controller.GetSite(key);
            TextBox txtUpdatedSiteName = (TextBox)this.gvSites.Rows[e.RowIndex].FindControl("txtUpdatedSiteName");
            site.SiteName = txtUpdatedSiteName.Text;
            controller.UpdateSite(site);
            gvSites.EditIndex = -1;
            PopulateSites();
        }

        void gvSites_RowEditing(object sender, GridViewEditEventArgs e)
        {
            e.Cancel = false;
            gvSites.EditIndex = e.NewEditIndex;
      //      gvSites.DataBind();
            PopulateSites();
        }

        void gvSites_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            e.Cancel = false;
            gvSites.PageIndex = e.NewPageIndex;
            PopulateSites();
        }

        void gvSites_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            e.Cancel = false;
            int key = (int)this.gvSites.DataKeys[e.RowIndex].Value;
             SiteController sitec = new SiteController();
                    sitec.DeleteSite(key);
                    PopulateSites();
        }

        void gvSites_RowDeleted(object sender, GridViewDeletedEventArgs e)
        {
            if (e.Exception != null)
            {
                e.ExceptionHandled = true;
            }
        }

        void gvSites_RowCommand(object sender, GridViewCommandEventArgs e)
        {

            int rowNumber = int.Parse(e.CommandArgument.ToString());
            SiteController controller = new SiteController();
            int key = (int) this.gvSites.DataKeys[rowNumber].Value;
            switch (e.CommandName)
            {
                case "Edit":
              //      gvSites.EditIndex = rowNumber;
                    gvSites.DataBind();
                    break;
                case "Delete":
                   ;
                    break;
                case "Update1":
                   
                    break;
                default:
                    break;

            }
            
        }

        void gvSites_RowUpdated(object sender, GridViewUpdatedEventArgs e)
        {
            if (e.Exception != null)
            {

            }
        }

        protected void lnkAddNewSite_Click(object sender, EventArgs e)
        {
            
        }

        protected void Unnamed1_Click(object sender, EventArgs e)
        {

           
            TakeoutExpress.Support.Linq.Site site;
           SiteController controller = new SiteController();
           site = controller.AddSite(txtNewSiteName.Text);
            
           txtNewSiteName.Text = string.Empty;
        //   pnlAddSitePopup.Visible = false;
           lnkAddNewSite_ModalPopupControlExtender.Hide();  
           gvSites.DataSource = controller.GetAllSites();
            gvSites.DataBind();
        }
    }
}
